package com.coin.controller;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.coin.exchange.bithumb.api.BithumbPrivateApi;
import com.coin.exchange.bithumb.api.BithumbPublicApi;
import com.coin.exchange.bithumb.vo.BithumbAccountRequestVO;
import com.coin.exchange.bithumb.vo.BithumbCancelRequestVO;
import com.coin.exchange.bithumb.vo.BithumbOrdersRequestVO;
import com.coin.exchange.bithumb.vo.BithumbPlaceRequestVO;
import com.coin.exchange.bithumb.vo.MarketBuyOrSellRequestVO;
import com.coin.exchange.bithumb.vo.OrderDetailRequestVO;
import com.coin.exchange.bithumb.vo.UserTickerRequestVO;
import com.coin.exchange.bithumb.vo.UserTransactionsRequestVO;
import com.coin.framework.bean.ResultForm;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/rest/bithumb")
public class BithumbController {

	private static Logger logger = LoggerFactory.getLogger(BithumbController.class);
	
	@Autowired
	private BithumbPublicApi bithumbPublicApi;

	@Autowired
	private BithumbPrivateApi bithumbPrivateApi;

	@ApiOperation(value = "获取交易所最后交易信息")
	@RequestMapping(value = "/getTicker", method = RequestMethod.POST)
	public ResultForm<?> getTicker(@RequestBody String currency) {
		ResultForm<?> resultForm = null;
//		String currency = jsonObject.getString("currency");
		try {
			String res = bithumbPublicApi.getTicker(currency);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "获取交易所销售/购买登记等待或交易中明细信息")
	@RequestMapping(value = "/getOrderbook", method = RequestMethod.POST)
	public ResultForm<?> getOrderbook(@RequestBody String currency) {
		ResultForm<?> resultForm = null;
//		String currency = jsonObject.getString("currency");
		try {
			String res = bithumbPublicApi.getOrderbook(currency);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "获取交易所交易签订完成明细")
	@RequestMapping(value = "/getRecentTransactions", method = RequestMethod.POST)
	public ResultForm<?> getRecentTransactions(@RequestBody String currency) {
		ResultForm<?> resultForm = null;
//		String currency = jsonObject.getString("currency");
		try {
			String res = bithumbPublicApi.getRecentTransactions(currency);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "获取交易所会员信息")
	@RequestMapping(value = "/getAccount", method = RequestMethod.POST)
	public ResultForm<?> getAccount(@RequestBody @Valid BithumbAccountRequestVO vo) {
		ResultForm<?> resultForm = null;
		String apiKey = vo.getApi_key();
		String secretKey = vo.getSecret_key();
		String currency = vo.getCurrency();
		try {
			String res = bithumbPrivateApi.getAccount(apiKey, secretKey, currency);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "获取交易所会员钱包信息")
	@RequestMapping(value = "/getBalance", method = RequestMethod.POST)
	public ResultForm<?> getBalance(@RequestBody @Valid BithumbAccountRequestVO vo) {
		ResultForm<?> resultForm = null;
		String apiKey = vo.getApi_key();
		String secretKey = vo.getSecret_key();
		String currency = vo.getCurrency();
		try {
			String res = bithumbPrivateApi.getBalance(apiKey, secretKey, currency);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "获取交易所会员存款地址")
	@RequestMapping(value = "/getWalletAddress", method = RequestMethod.POST)
	public ResultForm<?> getWalletAddress(@RequestBody @Valid BithumbAccountRequestVO vo) {
		ResultForm<?> resultForm = null;
		String apiKey = vo.getApi_key();
		String secretKey = vo.getSecret_key();
		String currency = vo.getCurrency();
		try {
			String res = bithumbPrivateApi.getWalletAddress(apiKey, secretKey, currency);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "获取会员最后交易信息")
	@RequestMapping(value = "/getUserTicker", method = RequestMethod.POST)
	public ResultForm<?> getUserTicker(@RequestBody @Valid UserTickerRequestVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String apiKey = vo.getApi_key();
			String secretKey = vo.getSecret_key();
			String order_currency = vo.getOrder_currency();
			String payment_currency = vo.getPayment_currency();
			String res = bithumbPrivateApi.getTicker(apiKey, secretKey, order_currency, payment_currency);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "获取销售/购买交易订单登记或进行中的交易")
	@RequestMapping(value = "/getOrders", method = RequestMethod.POST)
	public ResultForm<?> getOrders(@RequestBody @Valid BithumbOrdersRequestVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String apiKey = vo.getApi_key();
			String secretKey = vo.getSecret_key();
			String order_id = vo.getOrder_id();
			String type = vo.getType();
			Integer count = vo.getCount();
			Long after = vo.getAfter();
			String currency = vo.getCurrency();
			String res = bithumbPrivateApi.getOrders(apiKey, secretKey, order_id, type, count, after, currency);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "获取会员交易明细")
	@RequestMapping(value = "/getUserTransactions", method = RequestMethod.POST)
	public ResultForm<?> getUserTransactions(@RequestBody @Valid UserTransactionsRequestVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String apiKey = vo.getApi_key();
			String secretKey = vo.getSecret_key();
			Integer offset = vo.getOffset();
			Integer count = vo.getCount();
			Integer searchGb = vo.getSearchGb();
			String currency = vo.getCurrency();
			String res = bithumbPrivateApi.getUserTransactions(apiKey, secretKey, offset, count, searchGb, currency);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "获取会员销售/购买签订明细")
	@RequestMapping(value = "/getOrderDetail", method = RequestMethod.POST)
	public ResultForm<?> getOrderDetail(@RequestBody @Valid OrderDetailRequestVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String apiKey = vo.getApi_key();
			String secretKey = vo.getSecret_key();
			String order_id = vo.getOrder_id();
			String type = vo.getType();
			String currency = vo.getCurrency();
			String res = bithumbPrivateApi.getOrderDetail(apiKey, secretKey, order_id, type, currency);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "会员销售/购买交易订单登记及签订")
	@RequestMapping(value = "/place", method = RequestMethod.POST)
	public ResultForm<?> place(@RequestBody @Valid BithumbPlaceRequestVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String apiKey = vo.getApi_key();
			String secretKey = vo.getSecret_key();
			String order_currency = vo.getOrder_currency();
			String payment_currency = vo.getPayment_currency();
			Float units = vo.getUnits();
			Integer price = vo.getPrice();
			String type = vo.getType();
			String res = bithumbPrivateApi.place(apiKey, secretKey, order_currency, payment_currency, units, price, type);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "会员销售/购买交易取消")
	@RequestMapping(value = "/cancel", method = RequestMethod.POST)
	public ResultForm<?> cancel(@RequestBody @Valid BithumbCancelRequestVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String apiKey = vo.getApi_key();
			String secretKey = vo.getSecret_key();
			String order_id = vo.getOrder_id();
			String type = vo.getType();
			String currency = vo.getCurrency();
			String res = bithumbPrivateApi.cancel(apiKey, secretKey, type, order_id, currency);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "市场价购买")
	@RequestMapping(value = "/marketBuy", method = RequestMethod.POST)
	public ResultForm<?> marketBuy(@RequestBody @Valid MarketBuyOrSellRequestVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String apiKey = vo.getApi_key();
			String secretKey = vo.getSecret_key();
			Float units = vo.getUnits();
			String currency = vo.getCurrency();
			String res = bithumbPrivateApi.marketBuy(apiKey, secretKey, units, currency);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}

	@ApiOperation(value = "市场价销售")
	@RequestMapping(value = "/marketSell", method = RequestMethod.POST)
	public ResultForm<?> marketSell(@RequestBody @Valid MarketBuyOrSellRequestVO vo) {
		ResultForm<?> resultForm = null;
		try {
			String apiKey = vo.getApi_key();
			String secretKey = vo.getSecret_key();
			Float units = vo.getUnits();
			String currency = vo.getCurrency();
			String res = bithumbPrivateApi.marketSell(apiKey, secretKey, units, currency);
			JSONObject resultObject = JSONObject.parseObject(res);
			resultForm = ResultForm.createSuccessResultForm(resultObject, "成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			resultForm = ResultForm.createErrorResultForm(null, e.getMessage());
		}
		return resultForm;
	}
	
}
